//
// Created by Lenovo on 2020/10/5.
//
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
char num[maxn];
int main(){
    int t;
    cin>>t;
    while(t--)
    {
        int k;
        cin>>k;
        int n;
        cin>>n;
        int cnt = 0;
        n--;
        while(1)
        {
            int tmp = n%k;
            num[cnt++] = '0'+tmp;
            n = n/k;
            n--;
            if(n==0)
            {
                num[cnt++] = '0';
                break;
            }
            else if(n<0) break;
        }
        num[cnt] = '\0';
        for(int i = cnt-1;i>=0;i--) printf("%c",num[i]+(10-k));
        printf("\n");
    }

}